# -*- coding: utf-8 -*-
"""
用户认证相关接口的Swagger文档定义
"""

# 全局认证信息
AUTH_DOC = {
    "securityDefinitions": {
        "ApiKeyAuth": {
            "type": "apiKey",
            "in": "header",
            "name": "Authorization",
            "description": "在请求头中添加 'Authorization: Bearer {token}' 进行认证"
        },
        "CookieAuth": {
            "type": "apiKey",
            "in": "cookie",
            "name": "session",
            "description": "通过Cookie中的session进行认证"
        }
    },
    "security": [
        {
            "ApiKeyAuth": [],
            "CookieAuth": []
        }
    ]
}

# 获取Token说明
GET_TOKEN_DOC = {
    "tags": ["认证说明"],
    "summary": "获取认证Token",
    "description": "获取认证Token的步骤说明",
    "responses": {
        "200": {
            "description": "认证说明",
            "schema": {
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string",
                        "example": "请按照文档步骤获取认证Token"
                    }
                }
            }
        }
    }
}

# 统一注册接口文档
REGISTER_DOC = {
    "tags": ["用户认证"],
    "summary": "统一用户注册",
    "description": """
    统一用户注册接口 - 一次性完成微信登录和手机号绑定
    
    **功能特点：**
    - 自动创建新用户（如果不存在）
    - 自动绑定微信手机号
    - 支持用户基本信息设置
    - 事务性操作，确保数据一致性
    
    **使用场景：**
    - 新用户首次注册
    - 老用户重新登录并绑定手机号
    - 简化前端调用流程
    
    **微信小程序端示例：**
    ```javascript
    // 1. 获取微信登录code
    wx.login({
      success: function(res) {
        if (res.code) {
          // 2. 获取手机号
          wx.getPhoneNumber({
            success: function(phoneRes) {
              // 3. 调用统一注册接口
              wx.request({
                url: 'https://115.190.76.158/api/v1/user/register',
                method: 'POST',
                data: {
                  code: res.code,
                  encryptedData: phoneRes.encryptedData,
                  iv: phoneRes.iv,
                  userInfo: {
                    nickName: '用户昵称',
                    avatarUrl: '头像URL',
                    gender: 1
                  }
                },
                success: function(registerRes) {
                  console.log('注册成功:', registerRes.data);
                }
              });
            }
          });
        }
      }
    });
    ```
    """,
    "parameters": [
        {
            "name": "body",
            "in": "body",
            "required": True,
            "description": "统一注册参数",
            "schema": {
                "type": "object",
                "required": ["code", "encryptedData", "iv"],
                "properties": {
                    "code": {
                        "type": "string",
                        "description": "微信小程序wx.login()获取的code",
                        "example": "081234567890123456789"
                    },
                    "encryptedData": {
                        "type": "string",
                        "description": "微信加密的手机号数据（从wx.getPhoneNumber获取）",
                        "example": "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
                    },
                    "iv": {
                        "type": "string",
                        "description": "加密算法的初始向量（从wx.getPhoneNumber获取）",
                        "example": "r7BXXKfRPe8kr1MZ"
                    },
                    "userInfo": {
                        "type": "object",
                        "description": "用户基本信息（可选）",
                        "properties": {
                            "nickName": {
                                "type": "string",
                                "description": "用户昵称",
                                "example": "张三"
                            },
                            "avatarUrl": {
                                "type": "string",
                                "description": "头像URL",
                                "example": "https://example.com/avatar.jpg"
                            },
                            "gender": {
                                "type": "integer",
                                "description": "性别：0=未知，1=男，2=女",
                                "example": 1
                            }
                        }
                    }
                }
            }
        }
    ],
    "responses": {
        "200": {
            "description": "注册成功",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer",
                        "example": 200,
                        "description": "状态码"
                    },
                    "message": {
                        "type": "string",
                        "example": "注册成功",
                        "description": "响应消息"
                    },
                    "data": {
                        "type": "object",
                        "description": "注册结果",
                        "properties": {
                            "userId": {
                                "type": "integer",
                                "example": 12345,
                                "description": "用户ID"
                            },
                            "openid": {
                                "type": "string",
                                "example": "oNeBb5K6r8...",
                                "description": "微信openid"
                            },
                            "sessionKey": {
                                "type": "string",
                                "example": "HyVFkGl5F5OQWJZZaNzBBg==",
                                "description": "会话密钥"
                            },
                            "isNewUser": {
                                "type": "boolean",
                                "example": True,
                                "description": "是否新用户"
                            },
                            "phone": {
                                "type": "string",
                                "example": "13800138000",
                                "description": "手机号码"
                            },
                            "userInfo": {
                                "type": "object",
                                "description": "用户完整信息",
                                "properties": {
                                    "id": {
                                        "type": "integer",
                                        "example": 12345,
                                        "description": "用户ID"
                                    },
                                    "openid": {
                                        "type": "string",
                                        "example": "oNeBb5K6r8...",
                                        "description": "微信openid"
                                    },
                                    "phone": {
                                        "type": "string",
                                        "example": "13800138000",
                                        "description": "手机号码"
                                    },
                                    "nickname": {
                                        "type": "string",
                                        "example": "用户昵称",
                                        "description": "用户昵称"
                                    },
                                    "avatar_url": {
                                        "type": "string",
                                        "example": "https://example.com/avatar.jpg",
                                        "description": "头像URL"
                                    },
                                    "gender": {
                                        "type": "integer",
                                        "example": 1,
                                        "description": "性别：0=未知，1=男，2=女"
                                    },
                                    "is_active": {
                                        "type": "boolean",
                                        "example": True,
                                        "description": "是否活跃"
                                    },
                                    "created_at": {
                                        "type": "string",
                                        "example": "2024-01-01 12:00:00",
                                        "description": "注册时间"
                                    }
                                }
                            },
                            "registerTime": {
                                "type": "string",
                                "example": "2024-01-01 12:00:00",
                                "description": "注册时间"
                            }
                        }
                    }
                }
            }
        },
        "400": {
            "description": "请求参数错误",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 400},
                    "message": {"type": "string", "example": "缺少必要参数：code, encryptedData, iv"}
                }
            }
        },
        "401": {
            "description": "微信登录失败",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 401},
                    "message": {"type": "string", "example": "微信登录失败: invalid code"}
                }
            }
        },
        "500": {
            "description": "服务器错误",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 500},
                    "message": {"type": "string", "example": "服务器错误"}
                }
            }
        }
    }
}

# 微信登录接口文档
LOGIN_DOC = {
    "tags": ["用户认证"],
    "summary": "微信小程序登录",
    "description": "使用微信小程序的code进行登录",
    "parameters": [
        {
            "name": "body",
            "in": "body",
            "required": True,
            "description": "登录请求参数",
            "schema": {
                "type": "object",
                "required": ["code"],
                "properties": {
                    "code": {
                        "type": "string",
                        "description": "微信小程序wx.login()获取的code",
                        "example": "081234567890123456789"
                    }
                }
            }
        }
    ],
    "responses": {
        "200": {
            "description": "登录成功",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer",
                        "example": 200,
                        "description": "状态码"
                    },
                    "message": {
                        "type": "string",
                        "example": "登录成功",
                        "description": "响应消息"
                    },
                    "data": {
                        "type": "object",
                        "description": "用户数据",
                        "properties": {
                            "userId": {
                                "type": "integer",
                                "example": 12345,
                                "description": "用户ID"
                            },
                            "openid": {
                                "type": "string",
                                "example": "oNeBb5K6r8...",
                                "description": "微信openid"
                            }
                        }
                    }
                }
            }
        },
        "400": {
            "description": "请求参数错误",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer",
                        "example": 400
                    },
                    "message": {
                        "type": "string",
                        "example": "缺少必要参数"
                    }
                }
            }
        }
    }
}

# 绑定手机号接口文档
BIND_PHONE_DOC = {
    "tags": ["用户认证"],
    "summary": "绑定微信手机号",
    "description": """
    绑定用户微信手机号
    
    **使用流程：**
    1. 用户先调用微信登录接口获取 `openid` 和 `sessionKey`
    2. 在微信小程序中调用 `wx.getPhoneNumber()` 获取加密的手机号数据
    3. 将加密数据发送到此接口进行解密和绑定
    
    **微信小程序端示例：**
    ```javascript
    wx.getPhoneNumber({
      success: (res) => {
        // res.encryptedData 和 res.iv 就是需要的加密数据
        console.log('加密数据:', res.encryptedData);
        console.log('初始向量:', res.iv);
      }
    });
    ```
    """,
    "parameters": [
        {
            "name": "body",
            "in": "body",
            "required": True,
            "description": "微信手机号绑定参数",
            "schema": {
                "type": "object",
                "required": ["openid", "encryptedData", "iv", "sessionKey"],
                "properties": {
                    "openid": {
                        "type": "string",
                        "description": "用户的微信openid（从登录接口获取）",
                        "example": "oNeBb5K6r8..."
                    },
                    "encryptedData": {
                        "type": "string",
                        "description": "微信加密的手机号数据（从wx.getPhoneNumber获取）",
                        "example": "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
                    },
                    "iv": {
                        "type": "string",
                        "description": "加密算法的初始向量（从wx.getPhoneNumber获取）",
                        "example": "r7BXXKfRPe8kr1MZ"
                    },
                    "sessionKey": {
                        "type": "string",
                        "description": "会话密钥（从登录接口获取）",
                        "example": "HyVFkGl5F5OQWJZZaNzBBg=="
                    }
                }
            }
        }
    ],
    "responses": {
        "200": {
            "description": "绑定成功",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer",
                        "example": 200,
                        "description": "状态码"
                    },
                    "message": {
                        "type": "string",
                        "example": "手机号绑定成功",
                        "description": "响应消息"
                    },
                    "data": {
                        "type": "object",
                        "description": "绑定结果",
                        "properties": {
                            "userId": {
                                "type": "integer",
                                "example": 12345,
                                "description": "用户ID"
                            },
                            "phone": {
                                "type": "string",
                                "example": "13800138000",
                                "description": "解密后的手机号码"
                            },
                            "userInfo": {
                                "type": "object",
                                "description": "用户完整信息",
                                "properties": {
                                    "id": {
                                        "type": "integer",
                                        "example": 12345,
                                        "description": "用户ID"
                                    },
                                    "openid": {
                                        "type": "string",
                                        "example": "oNeBb5K6r8...",
                                        "description": "微信openid"
                                    },
                                    "phone": {
                                        "type": "string",
                                        "example": "13800138000",
                                "description": "手机号码"
                            },
                            "nickname": {
                                "type": "string",
                                "example": "用户昵称",
                                "description": "用户昵称"
                            },
                            "avatar_url": {
                                "type": "string",
                                "example": "https://example.com/avatar.jpg",
                                "description": "头像URL"
                                    },
                                    "gender": {
                                        "type": "integer",
                                        "example": 1,
                                        "description": "性别：0=未知，1=男，2=女"
                                    },
                                    "is_active": {
                                        "type": "boolean",
                                        "example": True,
                                        "description": "是否活跃"
                                    },
                                    "created_at": {
                                        "type": "string",
                                        "example": "2024-01-01 12:00:00",
                                        "description": "注册时间"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "400": {
            "description": "绑定失败",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 400},
                    "message": {"type": "string", "example": "缺少必要参数"}
                }
            }
        },
        "404": {
            "description": "用户不存在",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 404},
                    "message": {"type": "string", "example": "用户不存在"}
                }
            }
        },
        "500": {
            "description": "服务器错误",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 500},
                    "message": {"type": "string", "example": "服务器错误"}
                }
            }
        }
    }
}

# 获取用户信息接口文档
GET_USER_INFO_DOC = {
    "tags": ["用户认证"],
    "summary": "获取用户信息",
    "description": "获取当前用户信息\n\n**🔐 需要用户Token认证**",
    "security": [{"Bearer": []}],
    "responses": {
        "200": {
            "description": "获取成功",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "integer",
                        "example": 200,
                        "description": "状态码"
                    },
                    "message": {
                        "type": "string",
                        "example": "获取成功",
                        "description": "响应消息"
                    },
                    "data": {
                        "type": "object",
                        "description": "用户详细信息",
                        "properties": {
                            "id": {
                                "type": "integer",
                                "example": 1,
                                "description": "用户ID"
                            },
                            "nickname": {
                                "type": "string",
                                "example": "张三",
                                "description": "用户昵称"
                            },
                            "avatar_url": {
                                "type": "string",
                                "example": "https://example.com/avatar.jpg",
                                "description": "头像URL"
                            },
                            "phone": {
                                "type": "string",
                                "example": "13800138000",
                                "description": "手机号码"
                            },
                            "gender": {
                                "type": "integer",
                                "example": 1,
                                "description": "性别：0=未知，1=男，2=女"
                            },
                            "is_active": {
                                "type": "boolean",
                                "example": True,
                                "description": "是否活跃"
                            },
                            "created_at": {
                                "type": "string",
                                "example": "2024-01-01 12:00:00",
                                "description": "注册时间"
                            }
                        }
                    }
                }
            }
        },
        "401": {
            "description": "未认证",
            "schema": {
                "type": "object",
                "properties": {
                    "code": {"type": "integer", "example": 401},
                    "message": {"type": "string", "example": "用户未登录"}
                }
            }
        }
    }
} 